package cn.jolyoulu.adminweb.service.impl;


import cn.jolyoulu.adminweb.entity.bo.form.SysRoleFormBO;
import cn.jolyoulu.adminweb.entity.bo.query.SysRoleQueryBO;
import cn.jolyoulu.adminweb.entity.vo.SysRoleVO;
import cn.jolyoulu.cmnservice.enums.CmnExpType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.jolyoulu.adminweb.service.ApiSysRoleService;
import cn.jolyoulu.cmnservice.entity.po.SysRole;
import cn.jolyoulu.cmnservice.serivce.SysMenuService;
import cn.jolyoulu.cmnservice.serivce.SysRoleService;
import cn.jolyoulu.common.mybatis.plus.plugin.page.JlPage;
import cn.jolyoulu.common.web.enums.GlobalExpType;
import cn.jolyoulu.common.web.excption.GlobalException;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author LuZhouJin
 * @Date 2023/3/17
 */
@Slf4j
@Service
public class ApiSysRoleServiceImpl implements ApiSysRoleService {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public JlPage<SysRoleVO> pageList(SysRoleQueryBO bo) {
        JlPage<SysRoleVO> res = JlPage.execute(bo.getPage(), bo.getPageSize(), () -> {
            LambdaQueryWrapper<SysRole> ew = new LambdaQueryWrapper<>();
            if (StringUtils.isNotBlank(bo.getRoleName())) {
                ew.like(SysRole::getRoleName, bo.getRoleName());
            }
            if (StringUtils.isNotBlank(bo.getRoleType())) {
                ew.eq(SysRole::getRoleType, bo.getRoleType());
            }
            ew.orderByDesc(SysRole::getRoleSort);
            List<SysRole> list = sysRoleService.list(ew);
            return list.stream().map(SysRoleVO::new).collect(Collectors.toList());
        });
        return res;
    }

    @Override
    public SysRoleVO getById(String id) {
        SysRole sysRole = sysRoleService.getById(id);
        return new SysRoleVO(sysRole);
    }

    @Override
    @Transactional
    public void add(SysRoleFormBO sysRoleFormBO) {
        //检查类型唯一性
        SysRole check = sysRoleService.getByRoleType(sysRoleFormBO.getRoleType());
        if (Objects.nonNull(check)) {
            throw new GlobalException(CmnExpType.SYSROLE_TYPE_ERROR);
        }
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleFormBO, sysRole);
        sysRole.fillUpdate().fillCreat();
        sysRoleService.save(sysRole);
    }

    @Override
    @Transactional
    public void update(SysRoleFormBO sysRoleFormBO) {
        //不能修改的属性设null
        sysRoleFormBO.setRoleType(null);
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleFormBO, sysRole);
        sysRole.fillUpdate();
        sysRoleService.updateById(sysRole);
    }

    @Override
    @Transactional
    public void delByIds(List<String> ids) {
        for (String id : ids) {
            sysRoleService.removeById(id);
        }
    }

    @Override
    public void allotMenu(String id, List<String> menuIds) {
        sysRoleService.allotMenu(id, menuIds);
    }

    @Override
    public List<String> getMenuIds(String id) {
        return sysMenuService.selectIdByRoleId(id);
    }
}
